package com.itheima.service.cargo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.cargo.FactoryDao;
import com.itheima.dao.cargo.ProductDao;
import com.itheima.domain.cargo.Factory;
import com.itheima.domain.cargo.Product;
import com.itheima.domain.cargo.ProductExample;
import com.itheima.service.cargo.ProductService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductDao productDao;
    @Autowired
    private FactoryDao factoryDao;

    @Override
    public void save(Product product) {
        Factory factory = factoryDao.selectByPrimaryName(product.getFactoryName());
        product.setFactoryId(factory.getId());
        productDao.insertSelective(product);
    }

    @Override
    public void update(Product product) {
        product.setInputTime(new Date());
        productDao.updateByPrimaryKeySelective(product);
    }

    @Override
    public void delete(String id) {
        productDao.deleteByPrimaryKey(id);
    }

    @Override
    public Product findById(String id) {
        return productDao.selectByPrimaryKey(id);
    }

    @Override
    public List<Product> findAll(ProductExample example) {
        return productDao.selectByExample(example);
    }

    @Override
    public PageInfo findByPage(int pageNum, int pageSize, ProductExample example) {
        PageHelper.startPage(pageNum,pageSize);
        List<Product> products = productDao.selectByExample(example);
        return new PageInfo(products,10);
    }
}
